articles = {}

function Article (t) 
   articles[t.tag] = t
end

function first_author (str) 
   local first_author_end = 
      string.find(str, " and ") or string.len(str)
   return string.sub(str,1,first_author_end)
end 

function other_authors (str) 
 if string.len(first_author(str)) < string.len(str)  
 then return "and others" 
 else return "" 
 end 
end

function print_first_authors () 
   for tag,entry in pairs (articles) do 
      if entry.author 
      then 
	 io.write(
	    string.format("%s, author %s %s\n", tag,
			  first_author(entry.author), 
			  other_authors(entry.author)))
      end 
   end 
end

function find_numbered_lncs_articles (act) 
   for t,e in pairs (articles) do 
      if e.series == "LNCS" and e.number
      then act (t, e.number)
      end
   end
end 

local lncs_table = {} 

function insert_new_lncs_article (t,n)
   if lncs_table[n] == nil 
   then lncs_table[n] = {} 
   end; 
   local current = lncs_table[n] ; 
   current[#current+1] = t; 
end
      

function print_index_table () 
   for n,t in pairs (lncs_table) do 
      for k,i in ipairs (t) do 
	 print (n, i)
      end
   end
end

Author_list_t = {} 
Author_list_t.j = 1 
Author_list_t.str = ""

function get_first_author (str) 
   Author_list_t.str = str
   if string.find(str, " *[a-zA-Z]+")
   then 
      local i,k = string.find(str, " and ")  
      Author_list_t.j = k or string.len(str)+1
      return string.sub(str, 1, Author_list_t.j-1)
   end
end

function get_next_author () 
   local j = Author_list_t.j 
   if string.find(Author_list_t.str, " *[a-zA-Z]+", j)
   then 
      local i,k = 
	 string.find(Author_list_t.str, " and ", j)
      Author_list_t.j = k or  (string.len(Author_list_t.str)+1)
      i = i or (string.len(Author_list_t.str)+1)
      return string.sub(Author_list_t.str, j, i-1)
   else 
      return nil
   end
end

local author_numbers = {}

function get_author_count( t )
	
	if t.author and get_first_author( t.author ) then
		
		local count = 1
		
		while get_next_author () do
			count = count + 1
		end
		
		return count
	
	end
	
	return 0

end 

function process_author_numbers()

	for _, v in pairs( articles ) do
	
		local author_count = get_author_count( v )
		
		if author_numbers[ author_count ] == nil then
			author_numbers[ author_count ] = {}
		end
		
		table.insert( author_numbers[ author_count ], v )
	
	end

end

function print_author_numbers()
	
	for count, list in pairs( author_numbers ) do
		print( string.format("There are %s articles with %s authors", #list, count ) )
	end
	
end

function generate_graph_author_numbers()
	
	local f = io.open("graph.txt", "w")
	local max_authors = 0
	
	for i=0, table.maxn( author_numbers ) + 3 do
	
		local authors = author_numbers[i]
		local output = i .. "\t:"
		
		if authors then
			local count = #authors
			max_authors = math.max( count, max_authors )
		
			output = output .. string.rep( "-", count )
		end
		
		f:write( output .. "\n" )
	
	end
	
	f:write("Count\t:")
	
	for i=0, max_authors,10 do
		
		f:write( i )
		f:write( string.rep( " ", 10 - string.len(i) ) )
		
	end
	
	f:close()
	
end


dofile("lua_input.bib") 

process_author_numbers()
print_author_numbers()
generate_graph_author_numbers()